<template>
  <div class="video-detail-container">
    <!-- 返回按钮 -->
    <div class="back-section">
      <el-button type="primary" @click="goBack">
        <i class="el-icon-back"></i> 返回
      </el-button>
    </div>

    <!-- 视频标题区域 -->
    <div class="video-header" v-if="videoDetail">
      <h1 class="video-title">{{ videoDetail.title }}</h1>
      <div class="video-meta">
        <span>浏览量: {{ videoDetail.viewCount || 0 }}</span>
        <span>发布时间: {{ formatDate(videoDetail.createTime) }}</span>
      </div>
    </div>

    <!-- 视频播放区域 -->
    <div class="video-player-section" v-if="videoDetail">
      <div class="video-wrapper">
        <video
          controls
          :src="videoDetail.videoUrl.trim()"
          autoplay
          class="video-player"
          style="width: 100%; height: auto;"
        />
      </div>
    </div>

    <!-- 视频描述区域 -->
    <div class="video-description-section" v-if="videoDetail">
      <div class="video-description">
        <p>{{ videoDetail.description || '暂无描述' }}</p>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-else-if="loading" class="loading-state">
      <el-loading v-loading="loading" element-loading-text="加载中..." />
    </div>

    <!-- 错误状态 -->
    <div v-else-if="error" class="error-state">
      <el-empty description="视频加载失败" />
      <el-button type="primary" @click="loadVideoDetail">重新加载</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { getVideoInfoById } from '@/api/video';
import { ElMessage } from 'element-plus';

const router = useRouter();
const route = useRoute();

// 状态管理
const videoDetail = ref(null);
const loading = ref(false);
const error = ref(false);

// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

// 加载视频详情
const loadVideoDetail = async () => {
  const videoId = route.params.id;
  if (!videoId) {
    error.value = true;
    ElMessage.error('无效的视频ID');
    return;
  }

  loading.value = true;
  error.value = false;
  try {
    const response = await getVideoInfoById(videoId);
    if (response.success && response.data) {
      videoDetail.value = response.data;
    } else {
      error.value = true;
      ElMessage.error('获取视频详情失败');
    }
  } catch (err) {
    console.error('视频详情请求异常:', err);
    error.value = true;
    ElMessage.error('网络异常，请重试');
  } finally {
    loading.value = false;
  }
};

// 返回上一页
const goBack = () => {
  router.back();
};

// 页面加载时获取视频详情
onMounted(() => {
  loadVideoDetail();
});
</script>

<style scoped>
.video-detail-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.back-section {
  margin-bottom: 20px;
}

.video-player-section {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.video-wrapper {
  max-width: 800px;
  margin: 0 auto;
}

.video-header {
  margin-bottom: 20px;
}

.video-title {
  font-size: 28px;
  margin-bottom: 12px;
  color: #1a5276;
  font-weight: bold;
  text-align: center;
}

.video-description-section {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin-top: 20px;
}

.video-meta {
  display: flex;
  gap: 20px;
  margin-bottom: 16px;
  font-size: 14px;
  color: #666;
}

.video-description {
  font-size: 16px;
  line-height: 1.6;
  color: #333;
}

.loading-state {
  text-align: center;
  padding: 60px;
}

.error-state {
  text-align: center;
  padding: 60px;
}

.video-meta {
  display: flex;
  justify-content: center; /* 水平居中 */
  gap: 20px;
  margin-bottom: 16px;
  font-size: 14px;
  color: #666;
}

@media (max-width: 768px) {
  .video-detail-container {
    padding: 10px;
  }
  
  .video-title {
    font-size: 22px;
  }
  
  .video-player-section {
    padding: 10px;
  }
  
  .video-description-section {
    padding: 15px;
  }
}
</style>